package com.iretailer.report.web.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReBusinessTime;
import com.iretailer.report.model.BusinessTimeMall;
import com.iretailer.report.model.BusinessTimes;
import com.iretailer.report.model.User;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/1/25 09:30
 * @Description:
 */
@RestController
public class BusinessTimeController extends BaseAction {

    @RequestMapping("/business/gridBusinessTime.action")
    public Map<String, Object> gridBusinessTime(@RequestBody ReBusinessTime rbt, HttpServletRequest request) {
        QueryOption queryOption = modelMapper.map(rbt, QueryOption.class);
         User user = SecurityUserUtil.currentUser(request);
        List<String> companyIds = null;
        if (!user.getRoleId().equals("-")) {
            companyIds = companyService.getTreeId(user.getCompanyId());
            companyIds.add(user.getCompanyId());
        }

        PageResult find = businessTimeService.gridBusinessTimes(companyIds, rbt.getCompanyName(), queryOption);
        return RMap.ok(find);
    }

    @RequestMapping("/business/gridMallBusinessTime.action")
    public Map<String, Object> gridMallBusinessTime(@RequestBody ReBusinessTime rbt,HttpServletRequest request) {
        QueryOption queryOption = modelMapper.map(rbt, QueryOption.class);
         User user = SecurityUserUtil.currentUser(request);
        String userId = user.getId();
        if (user.getRoleId().equals("-")) {
            userId = null;
        }
        PageResult find = businessTimeMallService.gridMallBusinessTime(userId, rbt.getCompanyName(), rbt.getMallName(), queryOption);
        return RMap.ok(find);
    }

    @RequestMapping("/business/saveOrUpdateBusinessTime.action")
    public Map<String, Object> saveOrUpdateBusinessTime(@RequestBody ReBusinessTime rbt) {
        BusinessTimes businessTimes = modelMapper.map(rbt, BusinessTimes.class);
        boolean result = businessTimeService.saveOrUpdate(businessTimes);
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/business/saveOrUpdateMallBusinessTime.action")
    public Map<String, Object> saveOrUpdateMallBusinessTime(@RequestBody ReBusinessTime rbt) {
        String mallId = rbt.getMallId();
        // 查询此营业时间关联案场
        List<BusinessTimeMall> businessTimeMalls = businessTimeMallService.list(new QueryWrapper<BusinessTimeMall>().lambda().eq(BusinessTimeMall::getBusinesstimeId, rbt.getBusinesstimeId()));
        BusinessTimes businessTimes = modelMapper.map(rbt, BusinessTimes.class);
        // 如果只有一个案场 则更新营业时间信息
        boolean result = false;
        if (businessTimeMalls.size() == 1) {
            businessTimes.setId(rbt.getBusinesstimeId());
            result = businessTimeService.saveOrUpdate(businessTimes);

        } else {
            // 如果有多个则新建一个
            result = businessTimeService.creatBusiness(businessTimes, rbt.getBusinesstimeId(), rbt.getMallId());
        }
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/business/deleteBusinessTimeByIds.action")
    public Map<String, Object> deleteBusinessTimeByIds(@RequestBody List<Long> ids) {
        boolean result = businessTimeService.deleteBusinessTimeByIds(ids);
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/business/businessTimeSaveMalls.action")
    public Map<String, Object> businessTimeSaveMalls(@RequestBody ReBusinessTime rbt) {
        boolean result = businessTimeService.saveBusinessTimeMalls(rbt.getId(), rbt.getMallIds());
        return RMap.resultBoolean(result);
    }


    @RequestMapping("/business/deleteBusinessTime.action")
    public Map<String, Object> deleteBusinessTime(@RequestBody List<String> ids) {
        boolean result = businessTimeService.deleteByIds(ids);
        if (result) {
            return RMap.ok();
        } else {
            return RMap.error(RMap.Code.CONNECTED, "associated_with_mall");
        }
    }
}
